#include "test.h"

namespace sfc::test {

struct Test {
  Str _name;
  void (*_func)();

  auto name() const -> Str {
    return _name;
  }

  void run() {
    auto s = this->name();
    io::println("\e[32m[>>]\e[0m {}", s);
    _func();
    io::println("\e[32m[<<]\e[0m {}", s);
  }
};

struct Tests {
  Test _buf[1024];
  usize _len;

  auto push(Test value) -> usize {
    _buf[_len] = value;
    _len += 1;
    return _len;
  }

  auto run(Str pattern) -> usize {
    for (auto i = 0u; i < _len; ++i) {
      _buf[i].run();
    }
    return 0;
  }
};

auto tests() -> Tests& {
  static auto res = Tests();
  return res;
}

auto run(Str pattern) -> usize {
  static auto& x = tests();
  return x.run(pattern);
}

auto _install(Str name, void (*func)()) -> usize {
  static auto& x = tests();

  auto test = Test{name, func};
  return x.push(test);
}

}  // namespace sfc::test
